package com.crazyread.controller;

import com.crazyread.comment.lang.Result;
import com.crazyread.dao.BooksMapper;
import com.crazyread.imple.BooksServicelmlp;
import com.crazyread.imple.StatisticServiceImpl;
import com.crazyread.model.domain.*;
import com.crazyread.service.BooksService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
@RestController
@RequestMapping("/book")
@Api(value = "书籍管理 controller" ,tags = {"书籍管理接口统计"})
public class BookController {
    @Autowired
    BooksServicelmlp booksServicelmlp;
    @Autowired
    BooksService booksService;
    @Autowired
    StatisticServiceImpl statisticService;
    @Autowired
    BooksMapper booksMapper;
    //    查询全部书籍
    @GetMapping("/findBook")
    @ApiOperation(value = "查询全部书籍")
    public Result findBook(){
        List<Book> book = booksService.selectByBook();
        return Result.succ(book);
    }
    //随机查询4条数据
    @GetMapping("/findRandomBook")
    @ApiOperation(value = "随机查询4条数据")
    public Result findRandomBook(){
        List<Book> books = booksServicelmlp.selectRandom();
        return Result.succ(books);
    }
    //    id查询章节
    @GetMapping("/findBookChapter/{Id}")
    @ApiOperation(value = "id查询章节")
    public BookChapter findBookChapter(@PathVariable(value = "Id") Integer id){

        return booksMapper.selectChapterById(id);
    }
    //章节页
    @GetMapping(value = "/page/{pageNum}")
    @ApiOperation(value = "章节分页")
    public Result selectBookChapterWithPage(@PathVariable(value = "pageNum") Integer pageNum,
                                         @RequestParam(value = "count",defaultValue = "1") int count){
        PageInfo<BookChapter> bookChapter =
                booksServicelmlp.selectBookWithPage(pageNum,count);

        return Result.succ(bookChapter);
    }
    //    书籍id查询书籍所有章节
    @GetMapping("/findBookIdChapter/{bookId}")
    @ApiOperation(value = "书籍id查询书籍所有章节")
    public Result findBookIdChapter(@PathVariable(value = "bookId") Integer id){
        List<BookChapter> bookChapters = booksService.selectByIdChapter(id);
        for (int i = 0 ; i<bookChapters.size() ;i++){
            BookChapter bookChapter = bookChapters.get(i);
            bookChapter.setContent(null);
        }
        return Result.succ(bookChapters);
    }

    //    id查询书籍
    @GetMapping("/findIdBook/{bookId}")
    @ApiOperation(value = "id查询书籍")
    public Result findIdBook(@PathVariable(value = "bookId") Integer id){
        Book book = booksServicelmlp.selectByIdBook(id);
        statisticService.updateStatistics(book);
        return Result.succ(book);
    }
    //    名称查询书籍
    @GetMapping("/findNameBook/{bookName}")
    @ApiOperation(value = "名称查询书籍")
    public Result findNameBook(@PathVariable(value = "bookName") String bookName){
        List<Book> books = booksServicelmlp.selectByNameBook(bookName);
        return Result.succ(books);
    }
    //查询分类
    @GetMapping("/findTypeBook")
    @ApiOperation(value = "查询一级分类下的二级分类")
    public Result findTypeBook(){
        List<BookType> bookTypes = booksService.selectByType();
        return Result.succ(bookTypes);
    }

    //     分类id查询分类下的所有书籍
    @GetMapping("selectTypeBookById/{bookTypeId}")
    @ApiOperation(value = "书籍二级分类id查询分类下所有书籍")
    public Result selectByIdTypeBook(@PathVariable("bookTypeId") Integer bookTypeId){
        BookType3 bookTypes = booksService.selectByIdTypeBook(bookTypeId);
        return  Result.succ(bookTypes);
    }



    @PostMapping("/uploadBookPic")
    @ApiOperation(value = "图片上传")
    public Result uploadBookPic(@RequestParam("file") MultipartFile file, ModelMap modelMap, int id) {
        if (file.isEmpty()){
            System.out.println("文件为空");
        }
//        获取文件名
        String fileName = file.getOriginalFilename();
//        后缀名
        String suffixname = fileName.substring(fileName.lastIndexOf("."));
//        上传后文件路径
        String filepath = "F:\\vue_obj\\GraduationProject\\upImage//";
//        新文件名 UUID.randomUUID()随机编号
        fileName = UUID.randomUUID() + suffixname;

        File dest = new File(filepath+fileName);
        if (!dest.getParentFile().exists()){
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);

        }catch (IOException e){
            e.printStackTrace();
        }
//        本地服务器访问路径
        String filename = "http://localhost:80/file/"+fileName;
        modelMap.addAttribute("filename",filename);
        System.out.println(filename);

        Book b = this.booksService.selectByIdBook(id);
        b.setPic(filename);

        this.booksService.updateByIdBook(b);
        return Result.succ(200,"图片上传成功",b);
    }

    //    查询我的书架
    @GetMapping("/findRankBook")
    @ApiOperation(value = "查询我的书架")
    public Result findBookIdChapter(){
        List<Book> books = booksServicelmlp.selectByRankBook();
        return Result.succ(books);
    }
    //    添加到我的书架
    @PostMapping("/InsertByIdBookRank")
    @ApiOperation(value = "加入书架")
    public Result InsertByIdBookRank(int id){
        booksServicelmlp.InsertByIdBookRank(id);
        Book b = booksService.selectByIdBook(id);
        b.setRackstate(1);
        booksMapper.updateByIdBook(b);
        return Result.succ("添加成功");
    }
    //    移出我的书架及批量删除
    @ResponseBody
    @DeleteMapping("/deleteByIdBatchRankBook")
    @ApiOperation(value = "移出我的书架及批量删除")
    public Result deleteByIdBatchRankBook(int[] ids){
            HashMap hashMap = new HashMap();
            if (ids!=null && ids.length!=0){
                for (int i=0 ;i< ids.length ;i++){
                    booksMapper.deleteBookRackById(ids[i]);
                    Book b = booksService.selectByIdBook(ids[i]);
                    b.setRackstate(0);
                    booksMapper.updateByIdBook(b);
                    hashMap.put(ids[i],"删除成功");
                }
                return  Result.succ("删除成功");
            }else {
                hashMap.put("","删除失败");
            }
        return  Result.succ(hashMap);
    }
    //id查询分类下的书籍
    @GetMapping("/findBookTypeRanking/{typeId}")
    @ApiOperation(value = "分类id查询分类所有书籍热度排行")
    public Result findBookTypeRanking(@PathVariable(value = "typeId") int typeId){
        List<BookTypeRanking> bookTypeRankingList = statisticService.selectBookTypeRanking(typeId);
        return Result.succ(bookTypeRankingList);
    }
    //id查询书籍热度
    @GetMapping("/findBookRanking")
    @ApiOperation(value = "查询所有书籍热度排行")
    public Result findBookRanking(){
         List<Book> bookList = statisticService.selectBookRanking();
        return Result.succ(bookList);
    }
}
